﻿using MvcApp01.Context;
using MvcApp01.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace MvcApp01.Controllers
{
    public class EmpleadosController : Controller
    {
        #region ABM

        private DataContext db = new DataContext();

        // GET: Empleados
        public ActionResult Index()
        {
            return View(db.Empleados.Include(c => c.Departamentos).ToList());
        }

        // GET: Empleados/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            
            Empleado empleado = db.Empleados.Find(id);

            if (empleado == null)
                return HttpNotFound();

            return View(empleado);
        }

        // GET: Empleados/Create
        public ActionResult Create()
        {
            Empleado empleado = new Models.Empleado();
            ViewBag.IdDepartamento = new SelectList(db.Departamentos, "Id", "NombreDepartamento", empleado.IdDepartamento);

            return View(empleado);
        }

        // POST: Empleados/Create
        [HttpPost]
        public ActionResult Create(Empleado empleado)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Empleados.Add(empleado);
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Empleados/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            Empleado empleado = db.Empleados.Find(id);

            if (empleado == null)
                return HttpNotFound();

            ViewBag.IdDepartamento = new SelectList(db.Departamentos, "Id", "NombreDepartamento", empleado.IdDepartamento);

            return View(empleado);
        }

        // POST: Empleados/Edit/5
        [HttpPost]
        public ActionResult Edit(Empleado empleado)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry<Empleado>(empleado).State = EntityState.Modified;
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Empleados/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            Empleado empleado = db.Empleados.Find(id);

            if (empleado == null)
                return HttpNotFound();

            ViewBag.IdDepartamento = new SelectList(db.Departamentos, "Id", "NombreDepartamento", empleado.IdDepartamento);

            return View(empleado);
        }

        // POST: Empleados/Delete/5
        [HttpPost]
        public ActionResult Delete(int? id, Empleado empleado)
        {
            try
            {
                if (id == null)
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

                empleado = db.Empleados.Find(id);

                if (empleado == null)
                    return HttpNotFound();

                db.Empleados.Remove(empleado);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        #endregion

        public ActionResult VistaLibre()
        {
            return View();
        }

        public ActionResult VistaFormulario()
        {
            return View();
        }

        public ActionResult VistaSentencias()
        {
            return View();
        }
    }
}
